

var isValid = function(s) {
    let stack = [];
    let map = {
        '(': ')',
        '[': ']',
        '{': '}'
    };
    for (let i = 0; i < s.length; i++) {
        let char = s[i];
        if (char in map) {  // 如果是左括号，则入栈
            stack.push(char);   //  入栈
        } else {//开始出现右括号
            if (stack.length === 0) { // no opening bracket
                return false;   // invalid
            }
            let last = stack.pop(); // pop  from stack
            if (map[last] !== char) { // not match
                return false;  // invalid
            }
        }
    }
    return stack.length === 0;  // stack is empty
};

let s = "()";